package com.core.ingenium.view.epadilla;


import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {
    public SendMail() {
        super();
    }

    //public static void main(String[] args) throws Exception{
    //  String to = "me@estebanpadilla.com";
    //  String subject = "Correo de prueba desde jeveloper";
    //  String body = "Hola este correo fue enviado desde jdeveloper por EPadilla, que tenga un buen dia.";
    //  System.out.println("the mail is sent: " + send(to, subject, body));
    //}

    public static boolean send(String to, String subject, String body) {
        boolean success = false;

        Authenticator auth = new MailAuthenticator();
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.trust", "*");

        Session session = Session.getInstance(props, auth);
        //session.setDebug(true);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("padilla.esteban@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(body);

            Transport.send(message);

            success = true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return success;
    }
}
